package com.onetech.adrewards.free.adapters;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.objects.ListGiftCardDTO;


public class ListGiftCardAdapter extends BaseAdapter {
	private class ExchangeViewHolder {	
		public TextView nameGiftCard;
		public TextView giftCardPrice;
		public TextView giftCardExchage;
		public ImageView imageGiftCard;
	}

	private Activity activity;
	private List<ListGiftCardDTO> listGiftCard;
	private static LayoutInflater inflater = null;

	public ListGiftCardAdapter(Activity _activity) {
		activity = _activity;
		listGiftCard = new ArrayList<ListGiftCardDTO>();
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void updateAppList(List<ListGiftCardDTO> _listAppDTOs) {
		listGiftCard.clear();
		listGiftCard.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	public void insertAppList(List<ListGiftCardDTO> _listAppDTOs) {
		listGiftCard.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listGiftCard.size();
	}

	@Override
	public ListGiftCardDTO getItem(int arg0) {
		if (listGiftCard == null) {
			return null;
		}
		return listGiftCard.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ExchangeViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_row_list_giftcard,
					parent, false);
			viewHolder = new ExchangeViewHolder();
			viewHolder.nameGiftCard = (TextView) convertView
					.findViewById(R.id.nameGitfCard);			
			viewHolder.giftCardPrice = (TextView) convertView
					.findViewById(R.id.giftCardPriceItem);
			viewHolder.giftCardExchage = (TextView) convertView
					.findViewById(R.id.giftCardExchage);
			viewHolder.imageGiftCard = (ImageView) convertView
					.findViewById(R.id.imageGiftCard);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ExchangeViewHolder) convertView.getTag();
		}
		final ListGiftCardDTO temp = listGiftCard.get(position);
		
		viewHolder.nameGiftCard.setText(String.valueOf(temp.getName()));
		viewHolder.giftCardPrice.setText(String.valueOf(temp.getPrice())+"円");		
		String data="ptでGET";
		viewHolder.giftCardExchage.setText(String.valueOf(temp.getDiscount())+data);
		ImageLoader.getInstance(activity).DisplayImage(
				temp.getImage(), viewHolder.imageGiftCard);		
		
		return convertView;
	}

}

